package dexter.core.coord;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dexter.util.PropertiesReader;


public class Controller extends HttpServlet {

	private static final long serialVersionUID = 6858971526115165549L;
	
	private static Coordinator coord;
	private static boolean started = false;
	private static boolean error = false;
	private static String errorPage = "";
	
	public Controller() {
        super();          
		System.out.println("Creating Dexter Controller ...");		
		if (!started) start();
	}
	
	public synchronized void start() {
		
	   started = true;
	   System.out.println("START CONTROLLER");
		
		//Read main parameters
		String config="config.properties";		
		System.out.println(config);
		Properties props=PropertiesReader.getProperties(config);
		
		if (props==null) {
			File f = new File(config);
			errorPage = errorPage("File not found : "+f.getAbsolutePath());
			error = true;
			started=false;
		}
		
		else {
		coord = new Coordinator(props);
		
		final int PORT = Integer.parseInt(props.getProperty("coord_port"));
		System.out.println("Coordinator: Opening port "+PORT);
		coord.start();
		
//		new Thread(){
//			public void run() {
//				int p = PORT;
//				    Monitor monitor = new Monitor(p);
//			}
//			
//		}.start();

		
		System.out.println("CONTROLLER RUNNING");
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if (error) {
			sendResponse(response,errorPage);
		}
		
		String status = Coordinator.getStatus();
		String page = statusPage(status);
		sendResponse(response,page);
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
		
	}
	

	private void sendResponse(HttpServletResponse response, String htmlPage) throws IOException{
		 response.setContentType("text/html");
	     PrintWriter out = response.getWriter();
	     out.println(htmlPage);
	}
	
	private String errorPage(String error) {
		
		StringBuffer htmlResposta = new StringBuffer();
		
		htmlResposta.append("<html>");
		htmlResposta.append("<body>");
		htmlResposta.append("<p>");
		htmlResposta.append("<font color=\"red\">");
		htmlResposta.append("ERROR: ");
		htmlResposta.append("</font>");
		htmlResposta.append("<font color=\"black\">");
		htmlResposta.append(error);
		htmlResposta.append("</font>");
		htmlResposta.append("</p>");
		htmlResposta.append("</body>");
		htmlResposta.append("</html>");
		return htmlResposta.toString();
	}
	

	private String statusPage(String message) {
		
		StringBuffer htmlResposta = new StringBuffer();
		
		htmlResposta.append("<html>");
		htmlResposta.append("<body>");
		htmlResposta.append("<h2>");		
		htmlResposta.append("Dexer Controller");				
		htmlResposta.append("</h2>");
		htmlResposta.append("<p>");		
		htmlResposta.append("Coordinator state : ");
		htmlResposta.append("<br/>");
		htmlResposta.append("<br/>");
		htmlResposta.append("<b>"+message+"</b>");
		htmlResposta.append("<br/>");
		htmlResposta.append("</p>");
		htmlResposta.append("</body>");
		htmlResposta.append("</html>");
		return htmlResposta.toString();
	}
	
}
